// pages/test-login/test-login.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindGetUserInfo: function (e) {
        var that = this;
        //此处授权得到userInfo
        wx.showLoading({
            title: '登录中...',
        })
        wx.login({
            success: function (ev) {
                console.log('登录成功')
                wx.getUserInfo({
                    withCredentials: true,
                    success: function (res) {
                        console.log('获取用户信息unionid')
                        console.log(res);
                        wx.request({
                            url: config.openIdUrl,
                            method: 'POST',
                            header: {
                                'site': config.site,
                                'os': config.os,
                                'token': wx.getStorageSync('token')
                            },
                            data: {
                                code: ev.code,
                                encryptedData: res.encryptedData,
                                iv: res.iv,
                                signature: res.signature,
                                rawData: res.rawData,
                                type:2

                            },
                            success: function (res) {
                                wx.hideLoading()
                                wx.setStorageSync('token', res.data.data.session_key)
                                wx.setStorageSync('unionid', res.data.data.unionid)
                                app.globalData.memberInfo = res.data.data;
                                wx.setStorageSync('is_author', true)
                                setTimeout(function(){
                                    wx.navigateBack({

                                    })
                                },200)
                                

                            }
                        })
                    },
                    fail: function () {

                    }
                })
            }
        })

    },
    bindLogin(params) {
        let that = this;
        wx.request({
            url: config.openIdUrl,
            method: 'POST',
            header: {
                'site': config.site,
                'os': config.os,
                'token': wx.getStorageSync('token'),
                type: 2
            },
            data: params,
            success: function (res) {
                console.log("成功获取用户数据")
                console.log(app.globalData.userInfo)
                console.log(app.globalData.memberInfo)
                wx.setStorageSync('token', res.data.token)
                wx.setStorageSync('unionid', res.data.unionid)
                if (res.data.token != '') {
                    app._getUserInfo()
                }
            }
        })
    }
})